package com.zp.framework.jpa.test;

import java.util.Date;
import java.util.List;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.zp.domain.User;
import com.zp.framework.jpa.IJpaDao;
import com.zp.util.MyException;

/**
 * test JPADao function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/7
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunJPA {
	private IJpaDao JpaDao;
	private static final Log logger = LogFactory.getLog(RunJPA.class);

	@Before
	public void init() {
		JpaDao = (IJpaDao) new ClassPathXmlApplicationContext(
				"configure/applicationContext.xml").getBean("jpaDao");
		logger.info("*********** testing to initial spring container ****************");
	}

	@After
	public void destory() {
		JpaDao = null;
		logger.info("*********** testing to destory JPADao instance ******************");
	}

    @Test
	@Transactional
	public void testSave() {
		logger.info("*********** begin testing to create user *****************");
		User user = new User();
		user.setName("Alex07");
		user.setCertNumber("123456790");
		user.setTypeCode("1111111111111111");;
		user.setGender("1");
		user.setRegisterTime(new Date());
		user.setUpdateTime(new Date());
		try {
			JpaDao.save(user);
		} catch (MyException e) {
			e.printStackTrace();
		}
		logger.info("*********** end testing to create user ******************");
	}

	@Test
	@Transactional
	public void testUpdate() {
		logger.info("*********** begin testing to update user *****************");
		String user_id = "ff80808143aaecf70143aaed02ca0002";
		User user = null;
		try {
			user = (User) JpaDao.findById(User.class, user_id);
			Date now = new Date();
			user.setBirthday(now);
			try {
				JpaDao.update(user);
			} catch (MyException e) {
				e.printStackTrace();
			}
			Assert.assertEquals(now, ((User) JpaDao.findById(User.class,
					user_id)).getBirthday());
			logger.info("*********** end testing to update user *****************");
		} catch (MyException e1) {
			e1.printStackTrace();
		}

	}

	@Test
	@Transactional
	public void testDeleteByIds() {
		logger.info("*********** begin testing to delete user by id *****************");
		String user_id = "402881e6440b6d2a01440b6d370e0004";
		try {
			JpaDao.deleteById(User.class, user_id);
			try {
				User user = (User) JpaDao.findById(User.class, user_id);
				Assert.assertNull(user);
				logger.info("*********** end testing to delete user by id *****************");
			} catch (MyException e) {
				e.printStackTrace();
			}

		} catch (MyException e1) {
			e1.printStackTrace();
		}

	}

	@Test
	@Transactional
	public void testDeleteByCriteria() {
		logger.info("*********** begin testing to delete user by criteria with none pages *****************");
		String jpql = "from User u where u.username=? and u.password=?";
		Object[] queryParams = { "Alex05", "123456" };
		try {
			JpaDao.deleteFromCriteria(jpql, queryParams);
			List<?> userList = JpaDao.getResultData(-1, -1, jpql, queryParams);
			Assert.assertEquals(0, userList.size());
			logger.info("*********** end testing to delete user by criteria with none pages *****************");
		} catch (MyException e) {
			e.printStackTrace();
		}

	}

	@Test
	public void testFindById() {
		logger.info("*********** begin testing to find user by id *****************");
		String user_id = "402881e6440b6a0901440b6a11e30002";
		try {
			User user = (User) JpaDao.findById(User.class, user_id);
			Assert.assertNotNull(user);
			logger.info("*********** end testing to find user by id *****************");
		} catch (MyException e) {
			e.printStackTrace();
		}

	}

	@Test
	public void testCount() {
		logger.info("*********** begin testing to count user record rows *****************");
		String jql = "select count(id) from User";
		long rows = JpaDao.getCount(jql, null);
		Assert.assertEquals(5, rows);
		logger.info("*********** end testing to count user record rows *****************");
	}

	@Test
	public void testGetResultData() {
		logger.info("*********** begin testing to get result for user with criteria order by username *****************");
		Object[] queryParam = { "Alex" };
		try {
			List<?> list = JpaDao.getResultData(1, 5,
					"from User u where u.username=? order by u.username asc",
					queryParam);
			Assert.assertEquals(1, list.size());
			logger.info("*********** end testing to get result for user with criteria order by username *****************");
		} catch (MyException e) {
			e.printStackTrace();
		}

	}
    @Test
	public void testFindByNativeSQL() {
		logger.info("*********** begin testing to get result for user with native sql order by username *****************");
		String sql = "select * from tb_user where user_password=? order by user_login_name desc";
		Object[] queryParams = { "123456" };
		List<?> list = JpaDao.findByNativeQuery(1, 5, sql, queryParams);
		Assert.assertEquals(3, list.size());
		logger.info("*********** end testing to get result for user with native sql order by username *****************");

	}
    @Test
	public void testGetResultByClass() {
		logger.info("*********** begin testing to get result for user using Class *****************");
		List<?> list = JpaDao.getResultData(User.class);
		Assert.assertEquals(3, list.size());
		logger.info("*********** end testing to get result for user using Class *****************");
	}
}
